package com.shop.operator.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.model.Operator;
import com.shop.operator.service.OperatorService;

@Controller
public class OperatorAction {

	@Resource(name = "operatorLogin")
	private OperatorService operatorService;

	@RequestMapping("operatorLogin")
	@ResponseBody
	public void operatorLogin(String opearName, String opearPassword, HttpServletRequest request,
			HttpServletResponse response) {

		Operator operator = null;
		try {
			operator = operatorService.operatorLogin(opearName, opearPassword);
			if (operator == null) {
				request.setAttribute("msg", "用户名或密码错误");
				request.getRequestDispatcher("operator/public/login.jsp").forward(request, response);

			} else {
				request.getSession().setAttribute("operator", operator);
				response.sendRedirect("toOperatorIndex.action");
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@RequestMapping("operatorLogout")
	public void loginOut(HttpServletRequest request,HttpServletResponse response){
		
		try {
			request.getSession().invalidate();
			response.sendRedirect("operator/public/login.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
